package com.qifan.alb2b.action.back;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qifan.alb2b.action.back.param.HotelQueryParam;
import com.qifan.alb2b.domain.CityInfoDomain;
import com.qifan.alb2b.domain.HotelInfoDomain;
import com.qifan.alb2b.service.CityInfoService;
import com.qifan.alb2b.service.HotelInfoService;
import com.qifan.alb2b.service.RoomInfoService;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ID;
import com.qifan.alb2b.util.Page;

@Controller
@RequestMapping("/admin/hotel")
public class HotelManageController{
	private HotelInfoService hotelInfoService;
	private RoomInfoService roomInfoService;
	public void setRoomInfoService(RoomInfoService roomInfoService) {
		this.roomInfoService = roomInfoService;
	}

	private CityInfoService cityInfoService;
	public void setHotelInfoService(HotelInfoService hotelInfoService) {
		this.hotelInfoService = hotelInfoService;
	}
	public void setCityInfoService(CityInfoService cityInfoService) {
		this.cityInfoService = cityInfoService;
	}
	@RequestMapping("/query.adm")
	@ResponseBody
	public Page query(DataGridModel dgm,HotelQueryParam uqp,String cityId) {
		//spring太给力了，可以自动装配两个对象  会自动的装返回的Map转换成JSON对象
		//uqp.setHotelCity(cityId);
		return hotelInfoService.query(dgm,uqp);
	}
	
	@RequestMapping("/add.adm")
	@ResponseBody
	public Map<String, Object> add(HotelInfoDomain hotelInfoDomain){
		CityInfoDomain cityInfoDomain=cityInfoService.getCityById(hotelInfoDomain.getCityId());
		Map<String, Object> map=new HashMap<String, Object>();
		try{
			hotelInfoDomain.setHotelId(ID.next()+"");//设置唯一ID
			hotelInfoDomain.setCityCn(cityInfoDomain.getCityCn());
			hotelInfoService.add(hotelInfoDomain);
			map.put("success", true);
		}catch(RuntimeException e){
			map.put("success", false);
			map.put("msg", "操作失败,"+e.getMessage());
		}
		return map;
	}
	
	@RequestMapping("/update.adm")
	@ResponseBody
	public Map<String, Object> update(HotelInfoDomain hotelInfoDomain,String cityCn){
		CityInfoDomain cityInfoDomain=cityInfoService.getCityByCn(cityCn);
		Map<String, Object> map=new HashMap<String, Object>();
		
			hotelInfoDomain.setCityCn(cityCn);
			hotelInfoDomain.setCityId(cityInfoDomain.getCityId());
			hotelInfoService.update(hotelInfoDomain);
			map.put("success", true);
		
		return map;
	}
	
	@RequestMapping("/del.adm")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("uid") String[] ids){
		Map<String, Object> map=new HashMap<String, Object>();
		roomInfoService.getRoomInfoByHotelId(ids);
		try{
			hotelInfoService.delete(ids);
			map.put("success", true);
		}catch(RuntimeException e){
			map.put("success", false);
			map.put("msg", "操作失败,"+e.getMessage());
		}
		return map;
	}
	
}
